package com.mec.web.y2012.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class ToolTipServlet
 */
@WebServlet("/ajax/toolTip")
public class ToolTipServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private Map<Integer, CourseData> courses = new HashMap<Integer, CourseData>();
	
    public ToolTipServlet() {
        super();
        CourseData augusta = new CourseData(72, 7290);
        CourseData pinehurst = new CourseData(65, 7214);
        CourseData standrews = new CourseData(70, 6566);
        CourseData baltusrol = new CourseData(60, 7392);
        
        courses.put(1, augusta);
        courses.put(2, pinehurst);
        courses.put(3, standrews);
        courses.put(4, baltusrol);
        
    }

    
	@Override
	public void init() throws ServletException {
		super.init();
		
		
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int key = Integer.valueOf(request.getParameter("key"));
		
		CourseData data = courses.get(key);
		
		PrintWriter out = response.getWriter();
		
		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
		
		out.println("<response>");
		out.println("<par>" + data.getPar() + "</par>");
		out.println("<length>" + data.getLength() + "</length>");
		out.println("</response>");
		
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	private class CourseData{
		private int par;
		private int length;
		public CourseData(int par, int length) {
			super();
			this.par = par;
			this.length = length;
		}
		public int getPar() {
			return par;
		}
		public void setPar(int par) {
			this.par = par;
		}
		public int getLength() {
			return length;
		}
		public void setLength(int length) {
			this.length = length;
		}
		
	}
	private static final Logger logger=  
			Logger.getLogger(ToolTipServlet.class);
}
